package com.multiple.sources.core.config;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;

import com.multiple.sources.core.bean.TenantDataSource;

public class Constants {

	public static final String DEFAULT_DATA_SOURCE_NAME= "dataSource";
	public static final String DataSourceType = null;
	public static final String DataType = null;
	
	public static DataSource buildDataSource(Map<String, Object> dsMap) {
		Object type = dsMap.get("type");
		if (type == null) {
			type = "com.alibaba.druid.pool.DruidDataSource";// 默认DataSource

		}
		Class<? extends DataSource> dataSourceType;

		try {
			dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
			String driverClassName = dsMap.get("driverClassName").toString();
			String url = dsMap.get("url").toString();
			String username = dsMap.get("username").toString();
			String password = dsMap.get("password").toString();
			DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
					.username(username).password(password).type(dataSourceType);
			return factory.build();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static DataSource buildDataSourceByTenant(TenantDataSource tenantDataSource) {
		// TODO Auto-generated method stub
		Object type = tenantDataSource.getDataSourceType();
		if (type == null) {
			type = "com.alibaba.druid.pool.DruidDataSource";// 默认DataSource

		}
		Class<? extends DataSource> dataSourceType;

		try {
			dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
			String driverClassName =tenantDataSource.getDataSourceDriverClassName();
			String url = tenantDataSource.getDataSourceUrl();
			String username =tenantDataSource.getDataSourceUsername();
			String password =tenantDataSource.getDataSourcePassword();
			DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
					.username(username).password(password).type(dataSourceType);
			return factory.build();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static DataSource buildDataSourceByJdbc(int type, String dataSourceTypeDb, String url, String userName,
			String password, String driverClassName) {
		System.out.println("数据库类型:"+type);
		if (dataSourceTypeDb == null) {
			dataSourceTypeDb = "com.alibaba.druid.pool.DruidDataSource";// 默认DataSource
		}
		Class<? extends DataSource> dataSourceType;
		try {
			dataSourceType = (Class<? extends DataSource>) Class.forName((String) dataSourceTypeDb);
			DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
					.username(userName).password(password).type(dataSourceType);
			return factory.build();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}


}
